import operateM from "./operateM";
export default {
  data() {
    return {
      operates: [
        {
          label: "包含",
          operate: "includes",
        },
        {
          label: "不包含",
          operate: "noIncludes",
        },
        {
          label: "等于",
          operate: "=",
        },
        {
          label: "不等于",
          operate: "!=",
        },
        {
          label: "大于",
          operate: ">",
        },
        {
          label: "小于",
          operate: "<",
        },
        {
          label: "以_开始",
          operate: "start",
        },
        {
          label: "不以_开始",
          operate: "noStart",
        },
        {
          label: "以_结尾",
          operate: "end",
        },
        {
          label: "不以_结尾",
          operate: "noEnd",
        },
      ],
      keys: {},
      debounce: 500,
      sortProp: {},
      currentPage: 1,
      filterData: [],
    };
  },
  methods: {
    changeM() {
      clearTimeout(this.timer);
      this.timer = setTimeout(() => {
        this.currentPage = 1;
        this.filterAllKeHasValue();
      }, 300);
    },

    filterAllKeHasValue() {
      let list = [];
      Object.keys(this.keys).forEach((key) => {
        let val = this.keys[key].value;
        if (val != null && val.trim() != "") {
          list.push({ key, val, operate: this.keys[key].operate });
        }
      });
      this.handleFilterKey(list);
    },
    handleFilterKey(list) {
      let listData = [...this.originList];
      for (let i = 0, len = list.length; i < len; i++) {
        listData = operateM[list[i].operate](
          listData,
          list[i].key,
          list[i].val
        );
      }
      this.filterData = listData;
      let keySortProp = Object.keys(this.sortProp);
      if (!Object.is(keySortProp.length, 0)) {
        this.sortMethod(keySortProp[0]);
      }
      this.list = [...this.filterData];
      if (!this.isHiddenSingle) {
        //代表有分页
        this.pagInationM();
      }
    },
    sortMethod(prop) {
      this.filterData.sort((a, b) => {
        if (this.sortProp[prop] === "ascending") {
          return parseInt(a[prop]) - parseInt(b[prop]);
        } else if (this.sortProp[prop] === "descending") {
          return parseInt(b[prop]) - parseInt(a[prop]);
        }
      });
    },
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.filterAllKeHasValue();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.filterAllKeHasValue();
    },

    getClass(val) {
      if (val == null || val == "" || val.trim() == null) {
        return "";
      }
      return "my-active";
    },
    pagInationM() {
      let list = [];
      let len = this.filterData.length;
      for (
        let i = (this.currentPage - 1) * this.pageSize;
        i < this.pageSize * this.currentPage;
        i++
      ) {
        if (i < len) {
          let item = this.filterData[i];
          this.$set(item, "index", i + 1);
          list.push(item);
        }
      }
      this.list = list;
    },
    changeColWidth(newWidth, oldWidth, column, event) {
      console.log(newWidth, oldWidth, column, event);

      this.$set(this.cacheWidth, column.label, newWidth);
      localStorage.setItem(
        "cacheWidth",
        JSON.stringify({ ...this.cacheWidth })
      );
    },
    getCacheWidth(label, width) {
      width = width || 120;
      if (this.cacheWidth) {
        return this.cacheWidth[label] || width;
      }
      return width;
    },
    noop() {},
  },
  computed: {
    isShowColumn() {
      return Object.keys(this.keys).length > 0;
    },
    isHiddenSingle() {
      return !!this.dataSource.noPagination;
    },
    pageSize: {
      get() {
        if (this.dataSource.noPagination) {
          return 99999;
        }
        return this.dataSource.pageSize || 500;
      },
      set(val) {
        this.dataSource.pageSize = val;
      },
    },
    total() {
      return this.filterData.length;
    },
  },
  mounted() {
    Promise.resolve().then(() => {
      let keys = Object.keys(this.originList[0]);
      let keyObject = {};
      keys.forEach((key) => {
        keyObject[key] = {
          key,
          operate: "includes",
          value: "",
        };
      });
      this.keys = keyObject;
    });
  },
};
